from rest_framework.generics import ListCreateAPIView
from rest_framework.permissions import IsAdminUser

from users.models import User
from meiduo_admin.serializers.users import UserSerializer, UserAddSerializer
from meiduo_admin.utils.paginator import PageNum


class UserView(ListCreateAPIView):
    """
    获取用户视图
    """
    # 指定用户
    permission_classes = [IsAdminUser]
    # # 查询集
    # queryset = User.objects.all()
    # 序列化器
    serializer_class = UserSerializer
    # 分页器
    pagination_class = PageNum

    # def get_serializer_class(self):
    #     if self.request.method == 'GET':
    #         return UserSerializer
    #     else:
    #         return UserAddSerializer

    def get_queryset(self):
        if self.request.query_params.get('keyword'):  # 不为空，说明是按条件查询
            return User.objects.filter(username__contains=self.request.query_params.get('keyword'))
        else:  # 说明是查询全部
            return User.objects.all()


